module TopNotify
  class Logger
    def initialize(device)
      @device = if device.respond_to?(:write)
        device
      else
        File.open(device, 'a')
      end
      @device.sync = true if @device.respond_to?(:sync)
    end

    def log(msg)
      @device.write(msg.force_encoding('ASCII-8BIT').force_encoding('UTF-8'))
    end

    def log_stream(data, msg = nil)
      content  = "# #{time}"
      content += " (#{msg})" if msg
      content += " [#{data}]\n"
      log content
    end

    def time
      Time.now.strftime('%Y-%m-%d %H:%M:%S')
    end
  end
end
